﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Lync.Model.Conversation;

namespace CommunicatorHistories.Managers
{
    public class LyncConversationWraper : IDisposable
    {
        protected EventHandler<ContextEventArgs> dataSent;
        protected EventHandler<ContextEventArgs> dataReceived;
        private Func<string, string, bool> sentMessage;
        private Func<string, string, bool> receiveMessage;
        private EventHandler<InitialContextEventArgs> dataInitReceived;
        private EventHandler<InitialContextEventArgs> dataInitSent;

        public Conversation Conversation { get; protected set; }

        public LyncConversationWraper(Conversation conversation, Func<string, string, bool> onMessageSent, Func<string, string, bool> onMessageReceived = null, bool autoregister=true)
        {
            this.Conversation = conversation;
            this.dataSent = new EventHandler<ContextEventArgs>(Conversation_ContextDataSent);
            this.dataReceived = new EventHandler<ContextEventArgs>(Conversation_ContextDataReceived);
            this.dataInitReceived = new EventHandler<InitialContextEventArgs>(Conversation_InitialContextReceived);
            this.dataInitSent= new EventHandler<InitialContextEventArgs>(Conversation_InitialContextSent);
            this.sentMessage = onMessageSent;
            this.receiveMessage = onMessageReceived == null ? onMessageSent : onMessageReceived;
            if(autoregister) RegisterHendlers();
        }

        public LyncConversationWraper RegisterHendlers()
        {
            this.Conversation.ContextDataSent += this.dataSent;
            this.Conversation.ContextDataReceived += this.dataReceived;
            this.Conversation.InitialContextReceived += this.dataInitReceived;
            this.Conversation.InitialContextSent += this.dataInitSent;
            return this;
        }

        void Conversation_InitialContextSent(object sender, InitialContextEventArgs e)
        {
            ProcessSentMessage(e.ApplicationData, "", e.ApplicationId);
        }

        void Conversation_InitialContextReceived(object sender, InitialContextEventArgs e)
        {
            ProcessDataRecived(e.ApplicationData, "", e.ApplicationId);
        }

        public void UnRegisterHendlers()
        {
            this.Conversation.ContextDataSent -= this.dataSent;
            this.Conversation.ContextDataReceived -= this.dataReceived;
        }

        protected virtual void Conversation_ContextDataReceived(object sender, ContextEventArgs e)
        {
            ProcessDataRecived(e.ContextData, e.ContextDataType, e.ApplicationId);
        }
        protected virtual void Conversation_ContextDataSent(object sender, ContextEventArgs e)
        {
            ProcessSentMessage(e.ContextData, e.ContextDataType, e.ApplicationId);
        }






        private void ProcessDataRecived(string contextData, string contextDataType, string applicationId)
        {
            foreach (var contact in Conversation.Participants)
            {
                this.sentMessage(contact.Contact.ToString(), contextData);
            }
        }

        
        private void ProcessSentMessage(string contextData, string contextDataType, string applicationId)
        {          
            foreach (var contact in Conversation.Participants)
            {
                this.sentMessage(contact.Contact.ToString(), contextData);
            }
        }


        public void Dispose()
        {
            UnRegisterHendlers();
        }
    }
}
